<?php

class GalactusViewCalendar extends GalactusView {

	function __construct($control) {
		GalactusView::__construct($control);
	}

	public function retrieveJS( ) {
		return '
			function galactus_view_calendar_show(timeslots) {

				$(".timeslot-start").change(function( ) {
					var timeslot_start = $(this);
					var timeslot_end = timeslot_start.parents(".row-fluid").find(".timeslot-end");
					var timeslot_comment = timeslot_start.parents(".row-fluid").find(".timeslot-comment");
					if(timeslot_start.val( ) != "") {
						var date_start = new Date(timeslot_start.val( ));
						timeslot_end.removeAttr("disabled");          
						timeslot_end.find("option").removeAttr("disabled");
						timeslot_end.find("option:last").prop("selected", true);;
						timeslot_end.find("option").each(function( ) {
							var date_end = new Date($(this).val( ));
							if(date_start >= date_end)
								$(this).attr("disabled", "disabled");
						});
						timeslot_comment.removeAttr("disabled");
					}
					else {
						timeslot_end.attr("disabled", "disabled");
						timeslot_end.val("");
						timeslot_comment.attr("disabled", "disabled");
						timeslot_comment.val("");
					}
				});

				$("#button-confirm").click(function( ) {
					$(".timeslot-comment").each(function( ) { $(this).val($(this).val( ) + " "); });
					$("#contentarea").load(
						"'.URL.'CALENDAR/ADD_EVENTS",
						$("#hidden-telescope-id, .timeslot-start, .timeslot-end, .timeslot-comment").serializeArray( )
					);
				});

				$("#button-clear").click(function( ) {
					$(".timeslot-start").find("option:first").prop("selected", true);
					$(".timeslot-start").trigger("change");
					$(".timeslot-comment").val("");
				});

				if(timeslots != undefined) {
					$(".timeslot-start option").each(function( ) {
						for(var i = 0; i < timeslots.length; ++i) {
							if($(this).val( ) == timeslots[i].date_start) {
								$(this).parent( ).val($(this).val( ));
								$(".timeslot-start").trigger("change");
								$(this).parents(".row-fluid").find(".timeslot-end").val(timeslots[i].date_end);
								$(this).parents(".row-fluid").find(".timeslot-comment").val(timeslots[i].comment);
							}
						}
					});
				}
				else {
					$(".timeslot-start option:first").prop("selected", true);
					$(".timeslot-start").trigger("change");
				}
			}
		';
	}

	public function show($telescope_id) {
		$telescope = reset($this->control->model->retrieveTelescope($telescope_id));
		$timeslots = $this->retrieveTimeslots(new DateTime('now'), 7);
		$timeslots_format = "";
		for($i = 1; $i <= $timeslots["days"]; ++$i) {
			$timeslots_format .= '<div class="row-fluid">';
			$timeslots_format .= '<div class="span3">'.$this->retrieveTimeslotsSelect($timeslots['slots'], 'timeslot-start', $timeslots['times']*($i-1), $timeslots['times']*$i).'</div>';
			$timeslots_format .= '<div class="span3">'.$this->retrieveTimeslotsSelect($timeslots['slots'], 'timeslot-end', $timeslots['times']*($i-1), $timeslots['times']*$i).'</div>';
			$timeslots_format .= '<div class="span6">'.'<input type="text" name="params[ ]" class="span12 timeslot-comment" maxlength="256" /></div>';
			$timeslots_format .= '</div>';
		}
		$json = $this->control->model->retrieveCalendar($telescope_id);
		$json = !empty($json)? json_encode($json) : "undefined";
		echo '
			<input type="hidden" name="params[ ]" id="hidden-telescope-id" value="'.$telescope["telescope_id"].'" />
			<div class="container-fluid">
				<div class="row-fluid">
					<div class="alert alert-info fade in span12">
						<button type="button" class="close" data-dismiss="alert">×</button>
						Você escolheu o telescópio <strong>'.$telescope["name"].'</strong>!
					</div>
				</div>
				<div class="row-fluid">
					<div class="span3"><i class="icon-calendar"></i> Início da Observação</div>
					<div class="span3"><i class="icon-calendar"></i> Fim da Observação</div>
					<div class="span6"><i class="icon-comment"></i> Comentário</div>
				</div>
				'.$timeslots_format.'
				<div class="row-fluid">
					<button type="button" class="btn btn-small offset8 span2" id="button-clear">Limpar</button>
					<button type="button" class="btn btn-primary btn-small span2" id="button-confirm">Confirmar</button>
				</div>
			</div>
			<script type="text/javascript">$(galactus_view_calendar_show('.$json.'));</script>
		';
	}

	protected function retrieveTimeslots($date, $days) {
		$format = "Y-m-d H:i:s";
		$timeslots = array( );
		$date_start = new DateTime($date->format($format));
		$date_end =  new DateTime($date->format($format));
		for($i = 0; $i < $days; ++$i) {
			$date_start->modify('+'.$i.' days');
			$date_end->modify('+'.($i+1).' days');
			$hours_start = new DateTime($date_start->format('Y-m-d').' 19:00:00');
			$hours_end = new DateTime($date_end->format('Y-m-d').' 04:45:00');
			while($hours_start <= $hours_end) {
				$slots[ ] = new DateTime($hours_start->format($format));
				$hours_start->modify('+15 minutes');
			}
			$date_start = new DateTime($date->format($format));
			$date_end =  new DateTime($date->format($format));
		}
		return array("slots" => $slots, "days" => $days, "times" => count($slots)/$days);
	}


	private function retrieveTimeslotsSelect($slots, $class, $start, $end) {
		$en = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
		$pt = array('Domingo', 'Segunda', 'Terça', 'Quarta', 'Quinta', 'Sexta', 'Sábado');
		$options = '<option value=""></option>';
		for($i = $start; $i < $end; ++$i)
			$options .= '<option value="'.$slots[$i]->format("Y-m-d H:i:s").'">'.str_replace($en, $pt, $slots[$i]->format("l, d/m/Y H:i")).'</option>';
		return '<select name="params[ ]" class="span12 '.$class.'">'.$options.'</select>';
	}

}
?>
